// 1.初始化3个班级的学生信息 使用2维数组来存储
// 每个班级是一个数组，数组中每一个对象都表示一个学生
let stuInfo = [
    [{
            "stuID": "1",
            "stuName": "bdy",
            "stuAge": "18",
            "stuGender": "male",
            "stuScore": "100",
            "className": "1班"
        },
        {
            "stuID": "2",
            "stuName": "luc",
            "stuAge": "23",
            "stuGender": "female",
            "stuScore": "89",
            "className": "1班"
        },
        {
            "stuID": "3",
            "stuName": "lc",
            "stuAge": "17",
            "stuGender": "male",
            "stuScore": "93",
            "className": "1班"
        },
        {
            "stuID": "4",
            "stuName": "xu",
            "stuAge": "19",
            "stuGender": "female",
            "stuScore": "91",
            "className": "1班"
        },
        {
            "stuID": "5",
            "stuName": "chao",
            "stuAge": "20",
            "stuGender": "male",
            "stuScore": "95",
            "className": "1班"
        }
    ],
    undefined, //表示没有班级
    [{
            "stuID": "1",
            "stuName": "wf",
            "stuAge": "18",
            "stuGender": "female",
            "stuScore": "85",
            "className": "2班"
        },
        // {
        //     "stuID": "2",
        //     "stuName": "xtf",
        //     "stuAge": "19",
        //     "stuGender": "female",
        //     "stuScore": "91",
        //     "className": "2班"
        // },
        {
            "stuID": "3",
            "stuName": "xj",
            "stuAge": "19",
            "stuGender": "male",
            "stuScore": "92",
            "className": "2班"
        },
        {
            "stuID": "4",
            "stuName": "ls",
            "stuAge": "21",
            "stuGender": "male",
            "stuScore": "94",
            "className": "2班"
        },
        {
            "stuID": "5",
            "stuName": "xjs",
            "stuAge": "22",
            "stuGender": "female",
            "stuScore": "89",
            "className": "2班"
        }
    ],
    [{
            "stuID": "1",
            "stuName": "yaj",
            "stuAge": "22",
            "stuGender": "male",
            "stuScore": "86",
            "className": "3班"
        },
        {
            "stuID": "2",
            "stuName": "xj",
            "stuAge": "25",
            "stuGender": "female",
            "stuScore": "89",
            "className": "3班"
        },
        {
            "stuID": "3",
            "stuName": "xiz",
            "stuAge": "24",
            "stuGender": "female",
            "stuScore": "80",
            "className": "3班"
        },
        {
            "stuID": "4",
            "stuName": "yhq",
            "stuAge": "26",
            "stuGender": "female",
            "stuScore": "92",
            "className": "3班"
        },
        {
            "stuID": "5",
            "stuName": "npa",
            "stuAge": "19",
            "stuGender": "male",
            "stuScore": "96",
            "className": "3班"
        }
    ],
    [] //空数组表示有班级，单数没有没有学生
];
// 注意：删除班级用delete，删除学生用splice
// 2.管理员账号
let admin = {
    name: "qwe",
    passWord: "123"
}
// 3.输入模块
let readline = require("readline-sync");
// 功能函数
// 1.展示班级号
function showClassNumber() {
    let str = ""; //用于拼接班级号  1.第1班  2.第2班 3.第3班 4.返回
    for (let i = 0; i < stuInfo.length; i++) {
        // 判断当前班级是否存在
        if (stuInfo[i]) {
            str += `${i+1}.第${i+1}班  `
        }
    }
    str += `${stuInfo.length+1}.返回`
    return str;

}
// 2.根据学生姓名查找学生
// 变量的用途，变量的数据类型
function queryStudent(studentName) {
    let arr = []; //用于存放找到的学生
    for (let i = 0; i < stuInfo.length; i++) {
        if (stuInfo[i]) {
            for (let j = 0; j < stuInfo[i].length; j++) {
                if (stuInfo[i][j].stuName == studentName) {
                    arr.push(stuInfo[i][j]);
                }
            }
        }

    }
    return arr;

}

// 3.根据学生的班级号和学号查找学生,返回学生对象
function queryStudent_number(classNumber, studentNumber) {
    for (let i = 0; i < stuInfo[classNumber - 1].length; i++) {
        if (studentNumber == stuInfo[classNumber - 1][i].stuID) {
            return stuInfo[classNumber - 1][i];
        }
    }
}
// 4.根据学生的班级号和学号查找学生，返回学生对象和该学生对象所对应的下标
function queryStudent_number2(classNumber, studentNumber) {
    for (let i = 0; i < stuInfo[classNumber - 1].length; i++) {
        if (studentNumber == stuInfo[classNumber - 1][i].stuID) {
            return [stuInfo[classNumber - 1][i], i];
        }
    }
}

function main() {
    // 登录
    let count = 3;
    while (count) {
        console.log("请输入用户名：");
        let userName = readline.question("");
        console.log("请输入密码：");
        let passWord = readline.question("");
        if (userName === admin.name && passWord === admin.passWord) {
            console.log("登录成功！欢迎进入学生管理系统！");
            let mainFlag = true;
            while (mainFlag) {
                console.log("请选择功能：1.查询 2.修改 3.增加 4.删除 5.转班 6.退出");
                let mainChoice = readline.question("") * 1;
                switch (mainChoice) {
                    case 1:
                        console.log("查询！");
                        let queryFlag = true;
                        while (queryFlag) {
                            console.log("请选择功能：1.班级查询 2.学生查询 3.返回");
                            let queryChoice = readline.question("") * 1;
                            switch (queryChoice) {
                                case 1:
                                    while (true) {
                                        console.log("请选择要查询的班级：", showClassNumber());
                                        let classNumber = readline.question("") * 1; //2
                                        if (stuInfo[classNumber - 1]) {
                                            // console.table:按照表格形式输出内容，数据类型一般是数组
                                            console.table(stuInfo[classNumber - 1]);
                                        } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                            console.log("没有该班级！");
                                        } else if (classNumber == stuInfo.length + 1) {
                                            break;
                                        } else {
                                            console.log("输入有误！请重新输入！");
                                        }
                                    }

                                    break;
                                case 2:
                                    console.log("请输入学生姓名：");
                                    let studentName = readline.question("");
                                    let studentArr = queryStudent(studentName);
                                    console.log(studentArr);
                                    if (studentArr.length) {
                                        console.table(studentArr);

                                    } else {
                                        console.log("没有找到该学生！");
                                    }


                                    break;
                                case 3:
                                    queryFlag = false;
                                    break;
                                default:
                                    console.log("输入有误！请重新输入！");
                            }
                        }

                        break;
                    case 2:
                        console.log("修改！");
                        while (true) {
                            console.log("请选择要修改学生所在的的班级：", showClassNumber());
                            let classNumber = readline.question("") * 1; //2
                            if (stuInfo[classNumber - 1]) {
                                // console.table:按照表格形式输出内容，数据类型一般是数组
                                console.table(stuInfo[classNumber - 1]);
                                console.log("请输入要修改学生的学号：");
                                let studentNumber = readline.question("");
                                let studentObj = queryStudent_number(classNumber, studentNumber);
                                if (studentObj) {
                                    // 找到该学生，进行内容修改
                                    console.table([studentObj]);
                                    let changeFlag = true;
                                    while (changeFlag) {
                                        console.log("请选择要修改的内容：1.姓名 2.性别 3.年龄 4.分数 5.返回");
                                        let changeChoice = readline.question("") * 1;
                                        switch (changeChoice) {
                                            case 1:
                                                console.log("请输入修改后的姓名：");
                                                let changeName = readline.question("");
                                                let nameReg = /^[a-z]{2,5}$/;
                                                if (nameReg.test(changeName)) {
                                                    studentObj.stuName = changeName;
                                                    console.table([studentObj]);
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }
                                                break;
                                            case 2:
                                                console.log("请输入修改后的性别：(male或者female)");
                                                let changeGender = readline.question("");
                                                let gemderReg = /^male|female$/;
                                                if (gemderReg.test(changeGender)) {
                                                    studentObj.stuGender = changeGender;
                                                    console.table([studentObj]);
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }

                                                break;
                                            case 3:
                                                console.log("请输入修改后的年龄：(16-60)");
                                                let changeAge = readline.question("");
                                                // ^1[89]|[2-5]\d|60$  18-60岁
                                                let ageReg = /^1[6789]|[2-5]\d|60$/;
                                                if (ageReg.test(changeAge)) {
                                                    studentObj.stuAge = changeAge;
                                                    console.table([studentObj]);
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }

                                                break;
                                            case 4:
                                                console.log("请输入修改后的分数：(1-100)");
                                                let changeScore = readline.question("");
                                                let scoreReg = /^([1-9][0-9]{0,1}|100)$/;
                                                if (scoreReg.test(changeScore)) {
                                                    studentObj.stuScore = changeScore;
                                                    console.table([studentObj]);
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }

                                                break;
                                            case 5:
                                                changeFlag = false;
                                                break;
                                            default:
                                                console.log("输入有误！请重新输入！");



                                        }
                                    }
                                } else {
                                    console.log("没有该学生！");
                                }


                            } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                console.log("没有该班级！");
                            } else if (classNumber == stuInfo.length + 1) {
                                break;
                            } else {
                                console.log("输入有误！请重新输入！");
                            }
                        }


                        break;

                    case 3:
                        console.log("增加！");
                        let addFlag = true;
                        while (addFlag) {
                            console.log("请选择功能：1.增加班级 2.增加学生 3.返回");
                            let addChoice = readline.question("") * 1;
                            switch (addChoice) {
                                case 1:
                                    stuInfo.push([]);
                                    console.log("增加班级成功！");
                                    break;
                                case 2:
                                    while (true) {
                                        console.log("请选择要增加学生的班级：", showClassNumber());
                                        let classNumber = readline.question("") * 1; //2
                                        if (stuInfo[classNumber - 1]) {
                                            // console.table:按照表格形式输出内容，数据类型一般是数组
                                            console.table(stuInfo[classNumber - 1]);
                                            // 输入学生信息
                                            let studentObj = {}; //创建学生对象，用于存放学生信息
                                            // 增加学生ID
                                            if (stuInfo[classNumber - 1].length) {
                                                // 找到该班级最后一个学生的ID，然后+1，就是新加入学生的ID
                                                let maxIndex = stuInfo[classNumber - 1].length - 1; //下标最大值
                                                studentObj.stuID = stuInfo[classNumber - 1][maxIndex].stuID * 1 + 1 + "";

                                            } else {
                                                studentObj.stuID = "1";
                                            }
                                            while (true) {
                                                console.log("请输入学生的姓名：");
                                                let addName = readline.question("");
                                                let nameReg = /^[a-z]{2,5}$/;
                                                if (nameReg.test(addName)) {
                                                    studentObj.stuName = addName;
                                                    break;

                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }

                                            }

                                            while (true) {
                                                console.log("请输入学生的性别：(male或者female)");
                                                let addGender = readline.question("");
                                                let gemderReg = /^male|female$/;
                                                if (gemderReg.test(addGender)) {
                                                    studentObj.stuGender = addGender;
                                                    break;
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }
                                            }


                                            while (true) {
                                                console.log("请输入修改后的年龄：(16-60)");
                                                let addAge = readline.question("");
                                                // ^1[89]|[2-5]\d|60$  18-60岁
                                                let ageReg = /^1[6789]|[2-5]\d|60$/;
                                                if (ageReg.test(addAge)) {
                                                    studentObj.stuAge = addAge;
                                                    break;
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }
                                            }



                                            while (true) {
                                                console.log("请输入修改后的分数：(1-100)");
                                                let addScore = readline.question("");
                                                let scoreReg = /^([1-9][0-9]{0,1}|100)$/;
                                                if (scoreReg.test(addScore)) {
                                                    studentObj.stuScore = addScore;
                                                    break;
                                                } else {
                                                    console.log("输入有误！请重新输入！");
                                                }
                                            }

                                            // 增加学生的班级号
                                            studentObj.className = classNumber + "班";
                                            // 把新增加的学生插入到班级中
                                            stuInfo[classNumber - 1].push(studentObj);
                                            console.table(stuInfo[classNumber - 1]);


                                        } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                            console.log("没有该班级！");
                                        } else if (classNumber == stuInfo.length + 1) {
                                            break;
                                        } else {
                                            console.log("输入有误！请重新输入！");
                                        }
                                    }


                                    break;
                                case 3:
                                    addFlag = false;
                                    break;
                                default:
                                    console.log("输入有误！请重新输入！");
                            }

                        }

                        break;
                    case 4:
                        console.log("删除！");
                        let reduceFlag = true;
                        while (reduceFlag) {
                            console.log("请选择功能：1.删除班级 2.删除学生 3.返回");
                            let reduceChoice = readline.question("") * 1;
                            switch (reduceChoice) {
                                case 1:
                                    while (true) {
                                        console.log("请选择要删除的班级：", showClassNumber());
                                        let classNumber = readline.question("") * 1; //2
                                        if (stuInfo[classNumber - 1]) {
                                            // console.table:按照表格形式输出内容，数据类型一般是数组
                                            console.table(stuInfo[classNumber - 1]);
                                            // 删除该班级
                                            delete stuInfo[classNumber - 1];
                                            console.log("删除班级成功！");
                                        } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                            console.log("没有该班级！");
                                        } else if (classNumber == stuInfo.length + 1) {
                                            break;
                                        } else {
                                            console.log("输入有误！请重新输入！");
                                        }
                                    }

                                    break;
                                case 2:
                                    while (true) {
                                        console.log("请选择要删除学生所在的的班级：", showClassNumber());
                                        let classNumber = readline.question("") * 1; //2
                                        if (stuInfo[classNumber - 1]) {
                                            // console.table:按照表格形式输出内容，数据类型一般是数组
                                            console.table(stuInfo[classNumber - 1]);
                                            console.log("请输入要删除学生的学号：");
                                            let studentNumber = readline.question("");
                                            let studentArr = queryStudent_number2(classNumber, studentNumber);
                                            // studentArr[学生对象，下标]
                                            if (studentArr) {
                                                // 找到该学生
                                                console.table([studentArr[0]]);
                                                // 删除该学生
                                                stuInfo[classNumber - 1].splice(studentArr[1], 1);
                                                console.table(stuInfo[classNumber - 1]);
                                                console.log("删除学生成功！");

                                            } else {
                                                console.log("没有该学生！");
                                            }


                                        } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                            console.log("没有该班级！");
                                        } else if (classNumber == stuInfo.length + 1) {
                                            break;
                                        } else {
                                            console.log("输入有误！请重新输入！");
                                        }
                                    }

                                    break;
                                case 3:
                                    reduceFlag = false;
                                    break;
                                default:
                                    console.log("输入有误！请重新输入！");

                            }
                        }

                        break;
                    case 5:
                        console.log("转班！"); //把学生从原理的班级中删除，然后插入到新的班级
                        while (true) {
                            console.log("请选择要转班学生所在的的班级：", showClassNumber());
                            let classNumber = readline.question("") * 1; //2
                            if (stuInfo[classNumber - 1]) {
                                // console.table:按照表格形式输出内容，数据类型一般是数组
                                console.table(stuInfo[classNumber - 1]);
                                console.log("请输入要转班学生的学号：");
                                let studentNumber = readline.question("");
                                let studentArr = queryStudent_number2(classNumber, studentNumber);
                                // studentArr[学生对象，下标]
                                if (studentArr) {
                                    // 找到该学生
                                    console.table([studentArr[0]]);
                                    // 删除该学生
                                    let delStudentArr = stuInfo[classNumber - 1].splice(studentArr[1], 1);
                                    // delStudentArr:表示被删掉的学生所组成的数组 [{}]
                                    console.log("请选择要转入的班级：", showClassNumber());
                                    let newclassNumber = readline.question("") * 1; //2
                                    if (stuInfo[newclassNumber - 1]) {
                                        // console.table:按照表格形式输出内容，数据类型一般是数组
                                        console.table(stuInfo[newclassNumber - 1]);
                                        // 修改转班学生的学号和班级号
                                        if (stuInfo[newclassNumber - 1].length) {
                                            // 找到该班级最后一个学生的ID，然后+1，就是新加入学生的ID
                                            let maxIndex = stuInfo[newclassNumber - 1].length - 1; //下标最大值
                                            studentArr[0].stuID = stuInfo[newclassNumber - 1][maxIndex].stuID * 1 + 1 + "";

                                        } else {
                                            studentArr[0].stuID = "1";
                                        }


                                        delStudentArr[0].className = newclassNumber + "班";
                                        // 把删掉的学生插入到新的班级
                                        stuInfo[newclassNumber - 1].push(delStudentArr[0]);
                                        // 展示转入班级所有学生
                                        console.table(stuInfo[newclassNumber - 1]);

                                    } else if (newclassNumber <= stuInfo.length && newclassNumber > 0 && stuInfo[newclassNumber - 1] == undefined) {
                                        console.log("没有该班级！");
                                    } else if (newclassNumber == stuInfo.length + 1) {
                                        break;
                                    } else {
                                        console.log("输入有误！请重新输入！");
                                    }




                                } else {
                                    console.log("没有该学生！");
                                }


                            } else if (classNumber <= stuInfo.length && classNumber > 0 && stuInfo[classNumber - 1] == undefined) {
                                console.log("没有该班级！");
                            } else if (classNumber == stuInfo.length + 1) {
                                break;
                            } else {
                                console.log("输入有误！请重新输入！");
                            }
                        }


                        break;
                    case 6:
                        console.log("退出！欢迎下次再来！");
                        mainFlag = false; //关闭循环开关
                        count = 0; //登录次数清零
                        break;
                    default:
                        console.log("输入有误！请重新输入！");
                }


            }
        } else {
            count--;
            console.log(`用户名或密码输入有误！请重新输入！您还剩下${count}次机会！`);
        }
    }

}
main();